/*
 * Created on 2005-4-11
 *
 */
package com.hd123.rumba.util;

import java.util.List;

import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.exception.ParameterException;

/**
 * 层级码4-4-4-4
 * 
 * @author caili
 */
public class LevelCode {
  private String rule;
  private int[] lens;

  /**
   * @param rule
   *          例如2233表示最多4级, 每级长度分别为2, 2, 3, 3
   * @param levelCode
   * @throws ParameterException
   */
  public LevelCode(String rule) {
    super();
    setRule(rule);
  }

  /**
   * 取得最大级别. 从0开始.
   * 
   * @return
   */
  public int getLevels() {
    return rule.length() - 1;
  }

  /**
   * 取得级别level的全代码长度. level从0开始.
   * 
   * @param level
   * @return
   */
  public int getLevelFullLength(int level) {
    return lens[level];
  }
  
  /**
   * 取得级别level的代码长度. level从0开始.
   * 
   * @param level
   * @return
   */
  public int getLevelLength(int level) {
    if (level == 0) return lens[0];
    else return lens[level]-lens[level-1];
  }

  /**
   * 取得code代表的层次级别. 级别从0开始. 如果code不满足层级码规则, 返回-1.
   * 
   * @param code
   * @return
   */
  public int getLevel(String code) {
    if (code != null) {
      for (int i = 0; i < lens.length; ++i) {
        if (lens[i] == code.length())
          return i;
      }
    }
    return -1;
  }

  /**
   * 取得code的父级代码. 如果code是第0级代码, 则返回null
   * @param code
   * @return
   */
  public String getParent(String code) {
    int level = getLevel(code);
    if (level <= 0)
      return null;
    return code.substring(0, lens[level - 1]);
  }

  // getter/setter

  public String getRule() {
    return rule;
  }

  public void setRule(String rule) {
    this.rule = rule;
    lens = new int[rule.length()];
    for (int i = 0; i < rule.length(); ++i)
      lens[i] = Integer.parseInt(rule.substring(i, i + 1));
    for (int i = 1; i < lens.length; ++i)
      lens[i] += lens[i - 1];
  }

  /**
   * 从code中剥离出第level级的代码. level从0开始. 如果level超出范围, 返回null.
   * @param code
   * @param level
   * @return
   */
  public String getLevelCode(String code, int level) {
    if (level < 0 || level > getLevels())
      return null;
    if (level == 0)
      return code.substring(0, lens[level]);
    else
      return code.substring(lens[level - 1], lens[level]);
  }
  
  /**
   * 根据父层级码得出子层级码的匹配模式. 例如, parentLevelId="1231", 在层级规则为"224"的情况下, 返回"1231____"
   * @param lc
   * @param parentLevelId
   * @return
   * @throws BusinessException
   */
  public String getSubLevelIdPattern(String parentLevelId)
      throws BusinessException {
    int level = getLevel(parentLevelId) + 1;
    int levelLength = getLevelLength(level);
    if (levelLength == 0)
      throw new BusinessException("超出了最大的层级" + getLevels());
    String pattern = StringUtil.duplicate("_", levelLength);
    if (parentLevelId != null)
      pattern = parentLevelId.concat(pattern);
    return pattern;
  }

  /**
   * 根据父层级码, 找list中最大的子层级码, +1后返回子层级码.
   * @param lc
   * @param parentLevelId
   * @param list 其元素为String
   * @return
   * @throws BusinessException
   */
  public String createSubLevelId(String parentLevelId, List list) throws BusinessException {
    int level = getLevel(parentLevelId) + 1;
    int levelLength = getLevelLength(level);
    if (levelLength == 0)
      throw new BusinessException("超出了最大的层级" + getLevels());
    String result = null;
    if (list.size() == 0) {
      result = StringUtil.duplicate("0", levelLength);
      if (parentLevelId != null)
        result = parentLevelId + result;
    } else {
      // 查找最大的层级码. 不用sql max
      String max;
      if (parentLevelId == null)
        max = StringUtil.duplicate("0", levelLength);
      else
        max = parentLevelId + StringUtil.duplicate("0", levelLength);
      for (int i = 0; i < list.size(); ++i) {
        String id = (String) list.get(i);
        if (id.compareTo(max) > 0)
          max = id;
      }
      max = getLevelCode(max, level);
      result = StringUtil.increaseString(max);
      if (parentLevelId != null)
        result = parentLevelId + result;
    }
    return result;
  }

  
}
